<?xml version="1.0" encoding="UTF-8"?>
<html>
    <head>
        <link type="text/css" rel="stylesheet" href="./css/template.css" />
        <link type="text/css" rel="stylesheet" href="./css/SyntaxHighlighter.css" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Les fichiers .jar</title>
        <script language="javascript" src="./js/shInit.js" />
        <script language="javascript" src="./js/shCore.js" />
        <script language="javascript" src="./js/shBrushCpp.js" />
        <script language="javascript" src="./js/shBrushCSharp.js" />
        <script language="javascript" src="./js/shBrushCss.js" />
        <script language="javascript" src="./js/shBrushDelphi.js" />
        <script language="javascript" src="./js/shBrushJava.js" />
        <script language="javascript" src="./js/shBrushJScript.js" />
        <script language="javascript" src="./js/shBrushPhp.js" />
        <script language="javascript" src="./js/shBrushPython.js" />
        <script language="javascript" src="./js/shBrushRuby.js" />
        <script language="javascript" src="./js/shBrushSql.js" />
        <script language="javascript" src="./js/shBrushVb.js" />
        <script language="javascript" src="./js/shBrushXml.js" />
        <keywords>archive, jar, manifest</keywords>
        <author>Séverin PARENT</author>
		<STYLE>
		p{text-align:justify}
		</STYLE>
    </head>
    <body>
        <div class="chapter">
            <h2>Introduction</h2>
            <p>
				The archive format Java, JAR enables  you combine multiple files into one thanks to a ZIP compression standard.
                <br />
					   Generally a JAR file contains the files. Class and the auxiliary  resources associated to the application or applet. 
                <br />
               The interest desn't  stops herer , of course, since this format offers many features such as the ability to execute (link to part execution) simply, or  automatically your application. 
            </p>
            <p>Here, the main advantages that brings the JAR files:</p>
            <ul>
                <p />
                <li>Security: it's possible to  sign digitallity the contents of the archive.</li>
                <p />
                <li>Speed in the downloads: if your applet is stored in JAR, the files class and associated resources to be able to be download by the browser with http on a single transaction without needing to open a new connection for each file.</li>
                <p />
                <li>Reduced size: JAR format can compress data (ZIP format)</li>
                <p />
                <li>Portability: the managing  of  the JAR files is a standard of the Java platform.</li>
                <p />
                <li>Locking of the packages: it's possible to lock the contents of a JAR file.</li>
                <p />
                <li>Execution: JAR format allows easy and quick execution of the files that it contains.</li>
                <p />
                <li>Extension: JAR format is used by extensions to the Java platform.</li>
            </ul>
            <p>
                Before we interest in advanced features such as execution or compressed files signature we will first study the basic principles, such as the creation of a JAR file (orders and options through an example).
                <br />
                We will see then how the extraction and visualization of our compressed files.
            </p>
            <p>The understanding of the Manifest file is essential, that's why a part  will be dedicated to before we consider on the execution of an application or an applet from JAR format.</p>
        </div>
        <div class="chapter">
            <h2>Creating a JAR filE</h2>
            <div class="subChapter">
                <h3>Orders and options</h3>
                <p>
                    The base command  for creation of a JAR file is very easy:
                    <br />
                </p>
                <div class="quote">
                    <span class="cmd_line_lvl">$&gt;</span>
                    <span class="cmd_line">jar cf nom-jar noms-fichiers</span>
                    <br />
                </div>
                <p>
                   Let's see the arguments and options of this command in detail:
                    <br />
                    First "nom-jar" is the name that will be given to your compressed file, "cf" 
					indicates that we want our compressed file will be created in a file, in this case the file "nom-jar".
                    <br />
                    The option "c" fit to the crétion and "f" to the file, their order doesn't matter, however, it's imperative that these options are not separated by a space.
                </p>
                <p>The argument "noms-Fichiers" is a list of files to be archived. 
				   There may be one or more files, in the second case, they will be separated by spaces. In the case of records, the content of these will be added to the archive. This command will create an archive of our files in the directory.</p>
                <p>Let's now analyse some options that you can add to your order:</p>
                <ul>
                    <p />
                    <li>- v: create an exit indicating the name of each file added to our archive</li>
                    <p />
                    <li>-0: Used so that the files are not compressed</li>
                    <p />
                    <li>-m: indicates that you don't want to create a default Manifestfile, we will increasingly detail files manifest later in this article</li>
                    <p />
                    <li>- c: You can't keep the tree of the file when we archive directories</li>
                </ul>
                <p>To better understand this tool, we will focus on an example</p>
                <center>
                    <b>Example</b>
                </center>
                <br />
                <p>Take an application Helloworld (original don't you think?) Organized this way:</p>
                <br />
                <center>
                    <img src="./images/exemple.bmp" />
                </center>
                <p>Our Helloworld directory contains a class file Helloworld.class and a file with the images used by the Application.</p>
                <p>Pour archiver ces fichiers vous pouvez utiliser la commande :</p>
                <div class="quote">
                    <span class="cmd_line_lvl">$&gt;</span>
                    <span class="cmd_line">jar cvf Helloworld.jar Helloworld.class images</span>
                    <br />
                </div>
                <p>
                    A JAR file Helloworld will be created in the current file, containing ours. class and the image directory and its contents.
                    <br />
                   The option "v" has allowed us to have this display:
                </p>
                <p>
                    adding: Helloworld.class (in=25) (out=20) (deflated 20%)
                    <br />
                    adding: images/ (in=0) (out=0) (stored 0%)
                    <br />
                    adding: images/bouton.gif (in=128) (out=130) (deflated -1%)
                    <br />
                    adding: images/bonhomme.gif (in=153) (out=156) (deflated -1%)
                    <br />
                </p>
                <p>We can see that our file is compressed, in fact the default option achieves compression it must therefore used the following command  not to compress our archive:</p>
                <div class="quote">
                    <span class="cmd_line_lvl">$&gt;</span>
                    <span class="cmd_line">jar -cvf Helloworld.jar Helloworld.class images</span>
                    <br />
                </div>
                <p>It is possible to use the symbol "*" to archive totality of the current directory as the following order:</p>
                <div class="quote">
                    <span class="cmd_line_lvl">$&gt;</span>
                    <span class="cmd_line">jar -cvf Helloworld.jar *</span>
                    <br />
                </div>
                <p>If you want to put all your file at the same level in your archive, you can use the -c option:</p>
                <div class="quote">
                    <span class="cmd_line_lvl">$&gt;</span>
                    <span class="cmd_line">jar cf Helloworld.jar Helloworld.class -c images</span>
                    <br />
                </div>
                <p>The contents of your file will be then :</p>
                <p>
                    META-INF/MANIFEST.MF
                    <br />
                    Helloworld.class
                    <br />
                    bouton.gif
                    <br />
                    bonhomme.gif
                    <br />
                </p>
                <p>La même commande sans l’option " -c " donnera :</p>
                <p>
                    META-INF/MANIFEST.MF
                    <br />
                    Helloworld.class
                    <br />
                    images/bouton.gif
                    <br />
                    images /bonhomme.gif
                </p>
            </div>
        </div>
        <div class="chapter">
            <h2>Extraction and Displaying :</h2>
            <div class="subChapter">
                <h3>Extraction</h3>
                <p>The command base for the extraction of a JAR file is as follows:</p>
                <div class="quote">
                    <span class="cmd_line_lvl">$&gt;</span>
                    <span class="cmd_line">jar xf nom-jar [noms-fichiers]</span>
                    <br />
                </div>
                <p>
                   nom-jar is the JAR file that we want to extract, the "x" allows for the extraction, and the  "f" option indicates the file name to extract will be spent on command line.
                    <br />
                   The argument "noms-fichiers" allows to designate which files you want to extract, if ever it not be used, the totality of the archive is then extracted.
                </p>
                <p>The files are copied from the archive into the current directory, the archive remains unchanged, however if there is already file with the same name in the directory, they will be erased.</p>
                <p>For example, if we want to extract our Helloworld.jar file:</p>
                <div class="quote">
                    <span class="cmd_line_lvl">$&gt;</span>
                    <span class="cmd_line">jar xf Helloworld.jar Helloworld.class images/bouton.gif images/bonhomme.gif</span>
                    <br />
                </div>
                <p>The "Helloworld.class" file is copied, the directory "images" is created and files "bouton.gif, bonhomme.gif" are copied inside.</p>
            </div>
            <div class="subChapter">
                <h3>Visualisation</h3>
                <p>The base command for displaying a JAR file is as follows:</p>
                <div class="quote">
                    <span class="cmd_line_lvl">$&gt;</span>
                    <span class="cmd_line">jar tf nom-jar</span>
                    <br />
                </div>
                <p>The option "t" indicates that we want to see the content of our archive, "f" indicates that the name of the archive will pass in argument and of course "nom-jar" is the name or the full path of the file you want to see.</p>
                <p>For our example this command will diplay	:</p>
                <p>
                    META-INF/MANIFEST.MF
                    <br />
                    Helloworld.class
                    <br />
                    images/
                    <br />
                    images/bouton.gif
                    <br />
                    images /bonhomme.gif
                    <br />
                </p>
                <p>The "v" option will get additional information:</p>
                <p>
                    256 Wed May 5 15:28:37 PDT 2004 META-INF/MANIFEST.MF
                    <br />
                    20 Wed May 5 15:28:37 PDT 2004 Helloworld.class
                    <br />
                    0 Wed May 5 15:28:37 PDT 2004 images/
                    <br />
                    130 Wed May 5 15:28:37 PDT 2004 images/cross.gif
                    <br />
                    156 Wed May 5 15:28:37 PDT 2004 images/not.gif
                </p>
            </div>
        </div>
        <div class="chapter">
            <h2>Conclusion</h2>
            <p>
                Therefore, we dicussed duringthis article the treatment of JAR files, it can be very helpful in Process of your life applications.
                <br />
                For further information please don't hesitate to contact me.
            </p>
        </div>
        <div class="chapter">
            <h2>Running a file JAR</h2>
            <p>Now that we have seen how we creates JAR files, and the means to dispose of some of their functionalities , we'll see how to run them.</p>
            <p>Take the case of an application that we want to launch from the command line, the order is as follows:</p>
            <div class="subChapter">
                <span class="cmd_line_lvl">$&gt;</span>
                <span class="cmd_line">java -jar nom-jar</span>
                <br />
            </div>
            <p>" - jar "tells the interpreter command that our application is archived.</p>
            <p>"nom-jar" is obviously the name or the path of our archive.</p>
            <p>If the execution is carried out properly from the command line,the Manifest default file must be modified as seen previously.</p>
            <p>Consider this example from the beginning in order to synthesize the entire process.</p>
            <p>
                Creation of the Manifest file (monManifest.txt):
                <br />
                <b>Main-Class: Helloworld.class</b>
            </p>
            <p>Creation of the archive :</p>
            <div class="subChapter">
                <span class="cmd_line_lvl">$&gt;</span>
                <span class="cmd_line">jar cmf monManifest.txt Helloworld.jar Helloworld.class images</span>
                <br />
            </div>
            <p>Exécution de l’archive:</p>
            <div class="subChapter">
                <span class="cmd_line_lvl">$&gt;</span>
                <span class="cmd_line">java -jar Helloworld.jar</span>
                <br />
            </div>
        </div>
        <div class="chapter">
            <h>understanding of the  Manifest file</h2>
            <p>The JAR archives offer many features such as locked package, electronic signature,establishment of extension for example. It's the Manifest file which allows it.</p>
            <p>This file contains information about the contents of the archive and offers many possibilities. Before we studie it, see first the contents of the  Manifest default file wich is created if you don't use the "m" option.</p>
            <p>In effect when you create an archive JAR, a Manifestn file default is added. 
				There can be only one Manifest by archive, its path is always the following:
                <br />
                <b>META-INF/MANIFEST.MF</b>
            </p>
            <p>The contents of this file is easy: Manifest-Version: 1.0, because it contains no information about other files, this information will be added if you want to use the advanced features of JAR files.</p>
            <p>It must be added information in this file, we will therefore lean on the main features and ways to implement them through the Manifest file.</p>
            <h4>Application</h4>
            <p>
                If your file contains all the data of  an application to allow the execution of this last.First we must add the "Main-Class" header followed by the name of the class:
                <br />
                <b>Main-Class: nom-classe</b>
            </p>
            <h4>Locking of the package</h4>
            <p>
                It is possible to ensure the locking of package, for it we used the "Name" haeder followed by the "Sealed" header:
                <br />
                <b>
                    Name: mesPackages/premier/
                    <br />
                    Sealed: true
                </b>
            </p>
            <p>The Name header took  the value of the path package, the "Sealed" header takes the value of "true". These two lines are not separated, in fact as long as there is no line between two headers, we consider that they are linked, the value of "true" applies to "mesPackages / premier /".</p>
            <h4>Some rules about this file :</h4>
            <ul>
                <li>The line Manifest-Version is always the first, the order of the rest of the header isn't important.</li>
                <li>The Name header must contain a name, a full path of a file or a URL.</li>
                <li>An header not understood is ignored.</li>
                <li>The headers are case sensitives.</li>
            </ul>
        </div>
        <div class="chapter">
            <h2>The Manifest file</h2>
            <p>During the first part we discussed the various options for creating a JAR file, we 
				will now see deeper into the "m" option which allows to treat the file Manifest.</p>
            <p>Indeed, if the "m" option is not specified, a META-INF/MANIFEST.MF file is then added to our archive.It's possible to use the advanced functions of our JAR files, as locked package by modifying this file:</p>
            <div class="subChapter">
                <span class="cmd_line_lvl">$&gt;</span>
                <span class="cmd_line">jar cmf fichier-manifest nom-jar noms-fichiers</span>
                <br />
            </div>
            <p>
               The principle of this command is the following:
                <br />
                A text file containing the information we want to add to the Manifest file by default must be before been created.It's the argument "manifest file", this command will thereforeduring the creation of our archive add our text file to the file by default.
                <br />
                The other options and argument of this command are detailed in the creation of a JAR file (link)
            </p>
            <p>Take as an example the locking of a package.</p>
            <p>We have 2 packages that we want to lock:</p>
            mesPackages/premier/
            <br />
            mesPackages/second/
            <p>In order to carry out this task we have to write a text file "package" containing the following lines:</p>
            <p>
                Name: mesPackages/premier/
                <br />
                Sealed: true
            </p>
            <p>
                Name: mesPackages/premier/
                <br />
                Sealed: true
            </p>
            <p>
                Our file must end by a new line or a return, so that it will be integrated into the default file without problems.
                <br />
                It is'nt necessary to write the entire Manifest file since our text will be added to Default file.
            </p>
            <p>The command to create our archive by modifying the Manifest with our "package" file will be the folowing:</p>
            <div class="subChapter">
                <span class="cmd_line_lvl">$&gt;</span>
                <span class="cmd_line">jar cmf package nom-jar noms-fichiers</span>
                <br />
            </div>
            <p>The file Manifest our archive therefore take this form:</p>
            <p>Manifest-Version: 1.0</p>
            <p>
                Name: mesPackages/premier/
                <br />
                Sealed: true
            </p>
            <p>
                Name: mesPackages/premier/
                <br />
                Sealed: true
            </p>
        </div>
    </body>
</html>

